﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Globalization;
using System.Security.Cryptography;

namespace HelperService
{
  public static class Util
  {
    internal static string myAccountName = "remotify";
    internal static string myAccountKey = "syKvd1TrzV6P6Hw1gtBCREOAhF72qRTaBCO8J3a53BOOSp4ZHXmajKoD+Yov+c7LG6P2SSHYvZjdcCPnotWG+g==";

    internal static HttpWebRequest GetBaseRequest(string uri, string operation)
    {
      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); // create queue
      request.Timeout = (int)TimeSpan.FromSeconds(30).TotalMilliseconds; // 30 second timeout
      request.ReadWriteTimeout = request.Timeout; // same as other value
      request.Method = operation; // we want to create a queue
      request.ContentLength = 0;
      request.Headers["x-ms-date"] = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture); // always use UTC date/time
      return request;
    }

    internal static HttpWebRequest GetBaseRequestTable(string uri, string operation)
    {
      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); // create queue
      request.Timeout = (int)TimeSpan.FromSeconds(30).TotalMilliseconds; // 30 second timeout
      request.ReadWriteTimeout = request.Timeout; // same as other value
      request.Method = operation; // we want to create a queue
      request.ContentLength = 0;
      request.Headers["x-ms-date"] = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture); // always use UTC date/time
      request.Headers["DataServiceVersion"] = "1.0;NetFx.";
      request.Headers["MaxDataServiceVersion"] = "1.0;NetFx.";
      return request;
    }

    internal static string GetBaseCanonizedString(HttpWebRequest request)
    {
      string stringToSign = request.Method + "\n" +              // "VERB", aka the http method
                String.Empty + "\n" +                                   // "Content-MD5", we not using MD5 on this request
                request.ContentType + "\n" +                            // "Content-Type"
                String.Empty + "\n" +                   // "Date", we know this is here because we added it
                "x-ms-date:" + request.Headers["x-ms-date"] + "\n" +  // "CanonicalizedHeaders", we only have one, so we'll do it manually
                "/" + myAccountName + request.RequestUri.AbsolutePath;        // "CanonicalizedResources", just storage account name and path for this request
      return stringToSign;
    }

    internal static string GetBaseCanonizedStringTable(HttpWebRequest request)
    {
      string stringToSign = request.Method + "\n" +              // "VERB", aka the http method
                String.Empty + "\n" +                                   // "Content-MD5", we not using MD5 on this request
                request.ContentType + "\n" +                            // "Content-Type"
                DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture) +"\n" +     // "Date", we know this is here because we added it
                "/" + myAccountName + request.RequestUri.AbsolutePath;        // "CanonicalizedResources", just storage account name and path for this request
      return stringToSign;
    }

    internal static void SignRequest(HttpWebRequest request, string canonizedString)
    {
      byte[] KeyAsByteArray = Convert.FromBase64String(myAccountKey);
      byte[] dataToMAC = System.Text.Encoding.UTF8.GetBytes(canonizedString);
      string computedBase64Signature = string.Empty;

      using (HMACSHA256 hmacsha1 = new HMACSHA256(KeyAsByteArray))
      {
        computedBase64Signature = System.Convert.ToBase64String(hmacsha1.ComputeHash(dataToMAC));
      }

      string AuthorizationHeader = string.Format(CultureInfo.InvariantCulture, "SharedKey {0}:{1}", myAccountName, computedBase64Signature);
      request.Headers.Add("Authorization", AuthorizationHeader);
    }
  }
}